package com.itheima.web.manager;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.domain.db.Admin;
import com.itheima.service.db.AdminService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.web.exception.BusinessException;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Component
public class AdminManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * uuid 相当于每一次用户的 唯一标识
     * @param uuid
     * @return
     */
    //生成验证码
    public LineCaptcha createCode( String uuid) {
        //1.生成图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

        //2.将图片所生成的验证码存入redis
        String code = lineCaptcha.getCode();
        System.out.println("验证码:" + code);
        //存入五分钟的验证码
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE + uuid,code, Duration.ofMinutes(5));

        return lineCaptcha;
    }

    @Reference
    private AdminService adminService;
    /**
     * 登录功能
     * //1.判断验证码
     * //2.如果验证码失败 抛出业务异常
     * //3.如果成功 根据用户名进行查询
     * //4.查询不到 抛出业务异常
     * //5.查询到了 比较密码
     * //6.密码比较失败 抛出业务异常
     * //7.密码比较成功 生成token
     * //8.将数据存入redis
     */
    public String login(String username, String password, String verificationCode, String uuid) throws BusinessException {
        //1.判断验证码
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);
        //2.如果验证码失败 抛出业务异常
        if(!StrUtil.equals(verificationCode , redisCode)){
            throw new BusinessException("验证码错误");
        }

        //3.如果成功 根据用户名进行查询
        Admin admin = adminService.findByUserName(username);
        //4.查询不到 抛出业务异常
        if(admin == null){
            throw new BusinessException("用户名错误");
        }
        //5.查询到了 比较密码
        //先加密
        String md5Pwd2 = SecureUtil.md5(password);
        System.out.println(md5Pwd2);
        //6.密码比较失败 抛出业务异常
        if(!StrUtil.equals(md5Pwd2 , admin.getPassword() )){
            throw new BusinessException("密码输入错误");
        }

        //7.密码比较成功 生成token
        Map map = new HashMap<>();
        map.put("id", admin.getId() );
        map.put("username" , admin.getUsername());
        String token = JwtUtil.createToken(map);
        //8.将数据存入redis
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE + uuid); //登录成功验证码没有存在的必要了
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token, "1" ,Duration.ofMinutes(30) );
        return token;
    }

    /**
     * 根据id查询admin的对象
     * @param id
     * @return
     */
    public Admin findById(Long id) {
        return adminService.findById(id);
    }

    /**
     * 登出
     * @param token
     * @return
     */
    public Admin loginout(String token) {
        //这个token不允许有Bearer存在
        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN + token);
        return null;
    }
}
